
# -*- coding: UTF-8 -*-
#xiaohei.python.seo.call.me:)
#win+python2.7.x
import urllib2
import sys
import time

from pythonDFCF import mongodbClient

reload(sys)
sys.setdefaultencoding('utf-8')
from bs4 import BeautifulSoup
def jd(url,code):
    flag=True
    page = urllib2.urlopen(url)
    html_doc = page.read()
    soup = BeautifulSoup(html_doc.decode('gb2312','ignore'))
    for k in soup.find_all('div',class_='cwzb'):#,string='更多'
        table=k.find_all('table')
        for i in table:
            tr = i.find_all('tr')
            row=1;
            for a in tr:
                #print(row)
                tr = a.find_all('td')
                rowcl=1;
                for b in tr:
                    bb=b.text
                    bc = bb.encode()
                    #print(bc)
                    #print(rowcl)
                    desc="";
                    codedes=""
                    if row==2:
                        if rowcl==1:
                            codedes="codename"
                            desc="名字"
                        if rowcl==2:
                            codedes="zsz"
                            desc="总市值"
                        if rowcl==3:
                            codedes="jzc"
                            desc="净资产"
                        if rowcl==4:
                            codedes="jlr"
                            desc="净利润"
                        if rowcl==5:
                            codedes="syl"
                            desc="市盈率"
                        if rowcl==6:
                            codedes="sll"
                            desc="市净率"
                        if rowcl==7:
                            codedes="mll"
                            desc="毛利率"
                        if rowcl==8:
                            codedes="jll"
                            desc="净利率"
                        if rowcl==9:
                            codedes="roe"
                            desc="roe"

                    if row==3:
                        if rowcl==1:
                            codedes="industry"
                            desc="行业"
                        if rowcl==2:
                            codedes="hypj_zsz"
                            desc="行业平均:总市值"
                        if rowcl==3:
                            codedes="hypj_jzc"
                            desc="行业平均:净资产"
                        if rowcl==4:
                            codedes="hypj_jlr"
                            desc="行业平均:净利润"
                        if rowcl==5:
                            codedes="hypj_syl"
                            desc="行业平均:市盈率"
                        if rowcl==6:
                            codedes="hypj_sll"
                            desc="行业平均:市净率"
                        if rowcl==7:
                            codedes="hypj_mll"
                            desc="行业平均:毛利率"
                        if rowcl==8:
                            codedes="hypj_jll"
                            desc="行业平均:净利率"
                        if rowcl==9:
                            codedes="hypj_roe"
                            desc="行业平均:roe"

                    if row==4:
                        if rowcl==1:
                            codedes="industryPM"
                            desc="行业排名"
                        if rowcl==2:
                            codedes="hypm_zsz"
                            desc="行业排名:总市值"
                        if rowcl==3:
                            codedes="hypm_jzc"
                            desc="行业排名:净资产"
                        if rowcl==4:
                            codedes="hypm_jlr"
                            desc="行业排名:净利润"
                        if rowcl==5:
                            codedes="hypm_syl"
                            desc="行业排名:市盈率"
                        if rowcl==6:
                            codedes="hypm_sll"
                            desc="行业排名:市净率"
                        if rowcl==7:
                            codedes="hypm_mll"
                            desc="行业排名:毛利率"
                        if rowcl==8:
                            codedes="hypm_jll"
                            desc="行业排名:净利率"
                        if rowcl==9:
                            codedes="hypj_roe"
                            desc="行业排名:roe"
                    if row==5:
                        bc= bc.replace(" ","")
                        bc=bc[0:6]
                        bc= bc.replace("公","")
                        if rowcl==1:
                            codedes="sfwsx"
                            desc="四分位属性"
                        if rowcl==2:
                            codedes="sfwsx_zsz"
                            desc="四分位属性:总市值"
                        if rowcl==3:
                            codedes="sfwsx_jzc"
                            desc="四分位属性:净资产"
                        if rowcl==4:
                            codedes="sfwsx_jlr"
                            desc="四分位属性:净利润"
                        if rowcl==5:
                            codedes="sfwsx_syl"
                            desc="四分位属性:市盈率"
                        if rowcl==6:
                            codedes="sfwsx_sll"
                            desc="四分位属性:市净率"
                        if rowcl==7:
                            codedes="hypm_mll"
                            desc="四分位属性:毛利率"
                        if rowcl==8:
                            codedes="sfwsx_jll"
                            desc="四分位属性:净利率"
                        if rowcl==9:
                            codedes="sfwsx_roe"
                            desc="四分位属性:roe"
                    rowcl=rowcl+1
                    print(desc+":"+bc)
                    try:
                        if flag==True:
                          mongodbClient.insertMG(code,codedes,bc,desc)
                          flag=False
                          time.sleep(1) # 休眠1秒
                        else:
                          mongodbClient.insertAndUpdate(code,codedes,bc,desc)
                    except Exception as e:
                        mongodbClient.insertAndUpdate(code,codedes,bc,desc)
                        flag=False
                        print(e)
                    #time.sleep(1) # 休眠1秒
                row=row+1

#jd("http://quote.eastmoney.com/sh603993.html",603993)